package com.foreveross.crawl.adapter;

import java.util.Properties;

import com.foreveross.crawl.common.AdapterCfg;


/**
 * adapter路径
 * 
 * 位于lib下面/adapterClass/com/foreveross/crawl/adapter/sub/impl20130609
 * 
 * @author guokenye
 *
 */
public class AdapterInfo {
	
	
	private static Properties adapterCfg;
	private static String adapterClassSavePath;
	private static String adapterClassReadPath;
	private static String packPath;
	private static String root;
	private static String directory;
	private static long updateInterval;
	
	static {
		loadCfg();
		init();
	}
	
	private static void loadCfg(){
		AdapterCfg cfg;
		try{
			cfg=new AdapterCfg();
			cfg.load();
			adapterCfg=cfg.getProp();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			cfg=null;
		}
	}
	
	private static void init(){
		root=getRoot();
		directory=adapterCfg.getProperty("adapter.lib.directory");
		packPath=adapterCfg.getProperty("adapter.package.path");
		adapterClassSavePath=root+"/lib/"+directory+"/"+packPath.replaceAll("\\.", "/");
		adapterClassReadPath=root+"/lib/"+directory;
		updateInterval=Long.valueOf(adapterCfg.getProperty("adapter.update.interval"));
	}
	
	
	
	
	private static String getRoot(){
		String jarFilePath;
		String root;
		try {
			jarFilePath= 
					AdapterInfo.class.getProtectionDomain().getCodeSource().getLocation().getFile();
			/*jarFilePath = java.net.URLDecoder.decode(jarFilePath, "UTF-8");*/  
			java.io.File jarFile = new java.io.File(jarFilePath); 
			root=jarFile.getParent();
		}catch(Exception e){
			root="";
		}finally{
			jarFilePath=null;
		}   
		return root;
	}
	
	
	
	
	public static String getAdapterClassSavePath() {
		return adapterClassSavePath;
	}

	public static String getAdapterClassReadPath() {
		return adapterClassReadPath;
	}

	public static String getPackPath() {
		return packPath;
	}

	
	public static long getUpdateInterval() {
		return updateInterval;
	}

	
	public static void main(String[] args) {
		System.out.println(AdapterInfo.getAdapterClassReadPath());
		System.out.println(AdapterInfo.getAdapterClassSavePath());
		System.out.println(AdapterInfo.getPackPath());
		System.out.println(AdapterInfo.getUpdateInterval());
	}
}
